package com.example.amusic.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.amusic.MyApp
import com.example.amusic.R
import com.example.amusic.utils.LocalNavController
import com.example.amusic.utils.LottieAnimation
import com.example.amusic.utils.Screen
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberPermissionState

//@Composable
//fun AskPermissionScreen(
//    modifier: Modifier = Modifier,
//    shouldShowRationle: Boolean,
//    onRequestPermission: () -> Unit,
//    onOpenSettings: () -> Unit
//) {
//    LaunchedEffect(key1 = Unit) {
//        if (!shouldShowRationle) {
//            onRequestPermission()
//        }
//    }
//
//
//    Column(
//        modifier = modifier,
//        verticalArrangement = Arrangement.Center,
//        horizontalAlignment = Alignment.CenterHorizontally
//    ) {
//        Image(
//            modifier = Modifier
//                .size(96.dp)
//                .clip(RoundedCornerShape(14.dp)),
//            painter = painterResource(id = R.mipmap.icon_main),
//            contentDescription = ""
//        )
//        Spacer(modifier = Modifier.height(12.dp))
//        Text(
//            text = "需要申请文件权限",
//            style = MaterialTheme.typography.bodyLarge,
//            textAlign = TextAlign.Center
//        )
//        Spacer(modifier = Modifier.height(8.dp))
//        if (shouldShowRationle) {
//            Button(onClick = onRequestPermission) {
//                Text(text = "申请权限")
//            }
//        } else {
//            Button(onClick = onOpenSettings) {
//                Text(text = "打开系统设置")
//            }
//        }
//    }
//}

//借鉴 ：https://github.com/iZakirSheikh/Audiofy
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun AskPermissionScreen() {
    val controller = LocalNavController.current
    val permission = rememberPermissionState(permission = MyApp.STORAGE_PERMISSION) {
        if (!it) return@rememberPermissionState
        controller.graph.setStartDestination(Screen.MAIN.router)
        controller.navigate(Screen.MAIN.router) {
            popUpTo(Screen.PERMISSION.router) {
                inclusive = true
            }
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(
                horizontal = 32.dp, vertical = 16.dp
            ),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Box(
            modifier = Modifier
                .padding(bottom = 16.dp)
                .size(192.dp),
            propagateMinConstraints = true
        ) {
            LottieAnimation(id = R.raw.lt_permission, iterations = Int.MAX_VALUE)
        }
        Spacer(modifier = Modifier.height(16.dp))
        Text(text = stringResource(id = R.string.permission_screen_title))
        Spacer(modifier = Modifier.height(16.dp))
        Text(text = stringResource(id = R.string.permission_screen_desc))
        OutlinedButton(
            modifier = Modifier.size(width = 200.dp, height = 46.dp),
            onClick = { permission.launchPermissionRequest() },
            shape = CircleShape,
//                border = ButtonDefaults.outlinedBorder,
//                colors = ButtonDefaults.outlinedButtonColors(backgroundColor = Color.Transparent)
        ) {
            Text(text = stringResource(id = R.string.allow))
        }
    }

}


@Preview
@Composable
private fun PreviewAskPermissionScreen() {
    AskPermissionScreen()
}










